// pages/staff/check.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sncode: '',
    isDisable: false,
    record: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.getRecord(options.sncode);
  },

  getRecord: function (sncode) {
    var that = this;
    app.util.request({
      url: 'entry/wxapp/getRecordBySn',
      data: {
        sncode: sncode
      },
      cachetime: 0,
      success: function (res) {
        if (res.data.errno == 0) {
          that.setData({
            record: res.data.data
          });

        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
  },

  checkRecord: function () {
    var that = this;
    var user = wx.getStorageSync('user');
    that.setData({
      isDisable: true
    });
    if (that.data.record.merchantid == user.merchantid) {
      app.util.request({
        url: 'entry/wxapp/checkRecord',
        data: {
          recordid: that.data.record.id,
          merchantid: user.merchantid,
          staffid: user.id
        },
        cachetime: 0,
        success: function (res) {
          if (res.data.errno == 0) {
            wx.showToast({
              title: '核销成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack()
                }, 2000) //延迟时间
              }
            });
          }
        },
        fail: function (res) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      })
    } else {
      wx.showToast({
        title: '无权限核销该券',
        icon: 'none',
        duration: 2000
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})